<?php
namespace app\joybet\home;

use Braintree_Gateway;

/**
 *
 */
class Paypal extends Common
{
    protected $access_token = 'access_token$sandbox$qwrbb244qxqbn6q7$b2ab845bfdbf87ef2911590dace1249a';
    protected $gateway;

    public function __construct()
    {
        parent::__construct();
        $this->gateway = new Braintree_Gateway(array(
            'accessToken' => $this->access_token,
        ));
    }

    public function index()
    {
        
        $clientToken = $this->gateway->clientToken()->generate();

        $this->assign('clientToken', $clientToken);

        return $this->fetch();
    }

    public function notify()
    {
        $result = $this->gateway->transaction()->sale([
        "amount" => input('amount'),
        'merchantAccountId' => 'USD',
        "paymentMethodNonce" => input('nonce'),
        // "orderId" => $_POST['Mapped to PayPal Invoice Number'],
        // "descriptor" => [
        // "name" => "Descriptor displayed in customer CC statements. 22 char max"
        // ],
        // "shipping" => [
        // "firstName" => "Jen",
        // "lastName" => "Smith",
        // "company" => "Braintree",
        // "streetAddress" => "1 E 1st St",
        // "extendedAddress" => "Suite 403",
        // "locality" => "Bartlett",
        // "region" => "IL",
        // "postalCode" => "60103",
        // "countryCodeAlpha2" => "US"
        // ],
        // "options" => [
        // "paypal" => [
        // "customField" => $_POST["PayPal custom field"],
        // "description" => $_POST["Description for PayPal email receipt"]
        // ],
        // ]
        ]);

        if ($result->success) {
            echo '支付成功:订单号' . $result->transaction->id;
        } else {
            echo '支付失败:' . $result->message;
        }
    }
}
